home *** CD-ROM | disk | FTP | other *** search
- #! /usr/bin/env python
-
- # u1sdtool - command line utility for controlling ubuntuone-syncdaemon
- #
- # Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
- #
- # Copyright 2009 Canonical Ltd.
- #
- # This program is free software: you can redistribute it and/or modify it
- # under the terms of the GNU General Public License version 3, as published
- # by the Free Software Foundation.
- #
- # This program is distributed in the hope that it will be useful, but
- # WITHOUT ANY WARRANTY; without even the implied warranties of
- # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
- # PURPOSE. See the GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along
- # with this program. If not, see <http://www.gnu.org/licenses/>.
- from twisted.internet import glib2reactor
- glib2reactor.install()
-
- import dbus
- import codecs
- import os
- import sys
- import warnings
-
- from dbus.mainloop.glib import DBusGMainLoop
- from optparse import OptionParser
- from twisted.internet import reactor
-
- from ubuntuone.syncdaemon.tools import SyncDaemonTool
- from ubuntuone.syncdaemon.dbus_interface import DBUS_IFACE_NAME
- from ubuntuone.syncdaemon.tools import (
- show_path_info,
- show_uploads,
- show_downloads,
- show_shares,
- show_shared,
- )
-
-
- def main(argv, stdout):
- usage = "Usage: %prog [option]"
- parser = OptionParser(usage=usage)
- parser.add_option("-w", "--wait", dest="wait", action="store_true",
- help="Wait until ubuntuone-syncdaemon reachs nirvana")
- parser.add_option("", "--accept-share", dest="accept_share",
- metavar="SHARE_ID",
- help="accept the share with the specified id")
- parser.add_option("", "--reject-share", dest="reject_share",
- metavar="SHARE_ID",
- help="reject the share with the specified id")
- parser.add_option("", "--list-shares", dest="list_shares",
- action="store_true",
- help=" get the list of shares")
- parser.add_option("", "--refresh-shares", dest="refresh_shares",
- action="store_true",
- help=" request a refresh of the list of shares to"
- " the server")
- parser.add_option("", "--offer-share", dest="offer_share",
- metavar="PATH USER SHARE_NAME ACCESS_LEVEL",
- help=" share PATH to USER. ")
- parser.add_option("", "--list-shared", dest="list_shared",
- action="store_true",
- help=" list the shared path's/shares offered. ")
- parser.add_option("", "--refresh", dest="refresh_path",
- metavar="PATH", help=" request a refresh of PATH")
- parser.add_option("", "--info", dest="path_info",
- metavar="PATH", help=" request the metadata of PATH")
- parser.add_option("", "--current-transfers", dest="current_transfers",
- action="store_true",
- help=" show the current uploads and downloads")
- parser.add_option("-q", "--quit", dest="quit", action='store_true',
- help="shutdown the syncdaemon")
-
- (options, args) = parser.parse_args(argv)
-
- loop = DBusGMainLoop(set_as_default=True)
- bus = dbus.SessionBus(mainloop=loop)
- sync_daemon_tool = SyncDaemonTool(bus)
-
- # get the encoding of the output stream, defaults to UTF-8
- out_encoding = getattr(stdout, 'encoding', 'utf-8')
- if out_encoding is None:
- out_encoding = 'utf-8'
- out = codecs.getwriter(out_encoding)(stdout, errors='replace')
- if options.wait:
- def callback(result):
- """ wait_for_nirvana callback (stop the reactor and exit)"""
- out.write("\nubuntuone-syncdaemon became a fully "
- "enlightened Buddha!\n")
-
- d = sync_daemon_tool.wait_for_nirvana(verbose=True)
- d.addCallbacks(callback)
- elif options.list_shares:
- d = sync_daemon_tool.get_shares()
- d.addCallback(lambda r: show_shares(r, out))
- elif options.accept_share:
- d = sync_daemon_tool.accept_share(options.accept_share)
- elif options.reject_share:
- d = sync_daemon_tool.reject_share(options.reject_share)
- elif options.refresh_shares:
- d = sync_daemon_tool.refresh_shares()
- elif options.offer_share:
- if len(args) != 4:
- parser.error('--offer-share requires 4 arguments')
- d = sync_daemon_tool.offer_share(options.offer_share, *args[1:])
- elif options.list_shared:
- d = sync_daemon_tool.list_shared()
- d.addCallback(lambda r: show_shared(r, out))
- elif options.refresh_path:
- if not os.path.exists(options.refresh_path):
- parser.error("PATH: '%s' don't exists" % \
- options.refresh_path)
- d = sync_daemon_tool.query_by_path(options.refresh_path)
- elif options.path_info:
- if not os.path.exists(options.path_info):
- parser.error("PATH: '%s' don't exists" % \
- options.path_info)
- d = sync_daemon_tool.get_metadata(options.path_info)
- d.addCallback(lambda r: show_path_info(r, options.path_info, out))
- elif options.current_transfers:
- d = sync_daemon_tool.get_current_uploads()
- d.addCallback(lambda r: show_uploads(r, out))
- d.addCallback(lambda _: sync_daemon_tool.get_current_downloads())
- d.addCallback(lambda r: show_downloads(r, out))
- elif options.quit:
- d = sync_daemon_tool.quit()
- def shutdown_check(result):
- if result is None and \
- DBUS_IFACE_NAME in bus.list_names():
- out.write("ubuntuone-syncdaemon stopped.\n")
- else:
- out.write("ubuntuone-syncdaemon still running.\n")
- d.addBoth(shutdown_check)
- else:
- parser.print_help()
- sys.exit(1)
-
- def default_errback(error):
- """ default error handler. """
- out.write("\nOops, an error ocurred:\n")
- error.printTraceback()
-
- def stop_reactor(result):
- """ stop the reactor. """
- if reactor.running:
- reactor.stop()
- d.addErrback(default_errback)
- d.addCallback(stop_reactor)
- reactor.run()
-
-
- if __name__ == '__main__':
- # disable the dbus warnings
- warnings.filterwarnings('ignore', module='dbus')
- main(sys.argv, sys.stdout)
-
-